package xiugai.other;


import idcard.ocr.TessdOcrDemo;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.Word;
import org.opencv.core.Mat;
import org.opencv.utils.Converters;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class OCRUtil {
    /**
     * 识别图片信息
     * @param img
     * @return
     */
    public static String getImageMessage(BufferedImage img,String language){
        String result="end";
        try{
            result =  TessdOcrDemo.ocrResult(img);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(result);
        return result;
    }


    public static Mat readByChinese(String path){

        try{
            File file=new File(path);
            //获取输入流
            FileInputStream fis = new FileInputStream(file);

            //新的 byte 数组输出流，缓冲区容量1024byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            //缓存
            byte[] in = new byte[1024];
            int n;
            while ((n = fis.read(in)) != -1) {

                bos.write(in, 0, n);
            }
            //改变为byte[]
            byte[] data = bos.toByteArray();
            Vector<Byte> list = new Vector<>(data.length);
            for (byte b:data) {
                list.add(new Byte(b));
            }
            fis.close();
            bos.close();
                return Converters.vector_char_to_Mat(list);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        return null;






    }
}
